//
//  LeftViewVC.m
//  YZZX
//
//  Created by Easaa on 15/4/15.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "LeftViewVC.h"

#import "WebslitCell.h"

#import "CategoryDataModel.h"

@interface LeftViewVC ()<HXHttpRequestDelegate,UITableViewDelegate,UITableViewDataSource>
{
    HXHttpRequest *_httpRequest;            //请求
    UITableView *TableviewMassge;           //列表
    NSMutableArray *ArryDataCategory;            //左边菜单栏目 数据
    NSMutableArray *CategoryData;           //栏目数据

}

@end

@implementation LeftViewVC




- (void)viewDidLoad {
    [super viewDidLoad];
    
    //一个通知 栏目跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:)name:YZZX_LEFT_CELL_CHANG object:nil];
    
    ArryDataCategory = [[NSMutableArray alloc]init];
    CategoryData = [[NSMutableArray alloc]init];
    
    //self.view.backgroundColor = RGBCOLOR(250, 240, 240);
    
    UIImageView *iamgebackground = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 450/2, Dev_ScreenHeight)];
    iamgebackground.image = [UIImage imageNamed:@"Left_background_image"];
    [self.view addSubview:iamgebackground];
    
    [self DrawingPageForControlsUI];
    
    [self RequestHeadlLinesAndHeadImageDataFantion];
   
    

//    
//    NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
//    CategoryDataModel * mdoeldata;
//    
//    if(![UserDefaultSize integerForKey:YZZX_guanzhu_pindao])
//    {
//        mdoeldata = [cacheCateList objectAtIndex:0];
//        
//    }else if ([UserDefaultSize integerForKey:YZZX_guanzhu_pindao])
//    {
//        int Tagnsinter  = (int)[UserDefaultSize integerForKey:YZZX_guanzhu_pindao];
//        mdoeldata = [cacheCateList objectAtIndex:Tagnsinter];
//        
//        //创建通知
//        NSString *strtag =[NSString stringWithFormat:@"%d",self.mainController.selectedIndex];
//        NSDictionary *dicD =[[NSDictionary alloc]initWithObjectsAndKeys:strtag,@"Tag", nil];
//        
//        NSNotification *notification =[NSNotification notificationWithName:YZZX_LEFT_CELL_CHANG object:nil userInfo:dicD];
//        //通过通知中心发送通知
//        [[NSNotificationCenter defaultCenter] postNotification:notification];
//    }

 
 
 }

#pragma mark 通知执行函数
- (void)tongzhi:(NSNotification *)text
{
    
    NSLog(@"%@",text.userInfo[@"Tag"]);
    NSString * Tag = text.userInfo[@"Tag"];
    
  //跳转无组头，需要注意insection == 0；
    NSIndexPath *paths = [NSIndexPath indexPathForRow:[Tag integerValue] inSection:0];
    [TableviewMassge selectRowAtIndexPath:paths animated:YES scrollPosition:UITableViewScrollPositionNone];
}


//UI创建
-(void)DrawingPageForControlsUI
{
     
    
    self.iamgeview = [[UIImageView alloc]init];
    self.iamgeview.frame = CGRectMake(10, 64+10, 80, 80);
    [self.view addSubview:self.iamgeview];
   // self.iamgeview.image = [UIImage imageNamed:@"top_navgation@2x"];
 
    self.labletext = [[UILabel alloc]initWithFrame:CGRectMake(100+10, 64+10, 80, 80)];
     self.labletext.textAlignment = NSTextAlignmentCenter;
     self.labletext.font = [UIFont systemFontOfSize:14];
    // self.labletext.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.labletext];
    //  self.labletext.text = @"您好*妈妈";
    
    
    
    TableviewMassge = [[UITableView alloc]initWithFrame:CGRectMake(50, 164, 150, Dev_ScreenHeight-64-100)];
    TableviewMassge.delegate = self;
    TableviewMassge.dataSource =self;
    TableviewMassge.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:TableviewMassge];
    TableviewMassge.backgroundColor = [UIColor clearColor];
    
   
}






#pragma mark 请求大标题数据
-(void)RequestHeadlLinesAndHeadImageDataFantion
{
    
    //列表请求  参数为 用户ID  、 用户账号
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],                    @"user_id",
                              nil];
    
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_WEBSITE_LIST andTag:1001];
    
    //列表请求  参数为 用户ID  、 用户账号
    NSDictionary* dicParamsCategory = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],                   @"user_id",
                               @"1",                   @"site_id",nil];
    
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParamsCategory andMethod:YZ_CATEGORY_LIST andTag:1002];

    

    
    
    
}


#pragma mark Http request delegate
- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{

    //tableview列表数据

    if(tag == 1001||tag == 1003)
    {
        
        
        //NSLog(@"---------------->>>> %@",responseString);
    
        NSDictionary *DicJosn = [[NSDictionary alloc]init];
        DicJosn = [JsonUtil jsonToDic:responseString];
        
      //  NSLog(@"---------------->>>> \n%@",dic);
    
   
        NSString * strs = DicJosn[@"data"];
        NSDictionary *dicData =[JsonUtil jsonToDic:strs];
        
        
        NSArray *arryy =(NSArray *)dicData;
        
        
        
        // NSLog(@"---------00------->>>> %@",arryy);
        
        for (NSDictionary *dicModel in arryy) {
            
            // NSLog(@"------->>>> %@",[dicuu objectForKey:@"Image"]);
            
            WebsiteDataMoedel *cellmodel =[[WebsiteDataMoedel alloc]initWithDictionaryData:dicModel];
            [self.iamgeview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.hellomommy.cn%@",cellmodel.image_url]] placeholderImage:[UIImage imageNamed:@"bg"]];
            
            [self.iamgeview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.hellomommy.cn%@",cellmodel.image_url]]];
            
            [self.labletext setText:cellmodel.site_name];
        
        }
        
        
        
        //刷新
       // [TableviewMassge reloadData];

    
    
 
    }else if (tag ==1002)
    {
        
        //NSLog(@"---------------->>>> %@",responseString);
        
        NSDictionary *DicJosn = [[NSDictionary alloc]init];
        DicJosn = [JsonUtil jsonToDic:responseString];
        
        //  NSLog(@"---------------->>>> \n%@",dic);
        
        
        NSString * strs = DicJosn[@"data"];
        NSDictionary *dicData =[JsonUtil jsonToDic:strs];
        
        NSArray *arryy =(NSArray *)dicData;
 
        
       // NSLog(@"---------00------->>>> %@",arryy);
        
        for (NSDictionary *dicModel in arryy) {
            
            CategoryDataModel *CategoryModel =[[CategoryDataModel alloc]init];
            CategoryModel.image2_url =[dicModel objectForKey:@"image2_url"];
            CategoryModel.cate_name  =[dicModel objectForKey:@"cate_name"];
            CategoryModel.children   =[dicModel objectForKey:@"children"];
            CategoryModel.cate_id    =[dicModel objectForKey:@"cate_id"];
            CategoryModel.page_type  =[dicModel objectForKey:@"page_type"];
            CategoryModel.image_url  =[dicModel objectForKey:@"image_url"];
            CategoryModel.config     =[dicModel objectForKey:@"config"];
            CategoryModel.cate_type  =[dicModel objectForKey:@"cate_type"];
         
          
            [ArryDataCategory addObject:CategoryModel];
            NSLog(@"---------00------->>>> %@",[dicModel objectForKey:@"cate_name"]);
            
            [TableviewMassge reloadData];
            
          
            NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
            
            if(![UserDefaultSize integerForKey:YZZX_guanzhu_pindao])
            {
                
                //跳转无组头，需要注意insection == 0；
                NSIndexPath *paths = [NSIndexPath indexPathForRow:0 inSection:0];
                [TableviewMassge selectRowAtIndexPath:paths animated:YES scrollPosition:UITableViewScrollPositionNone];
                
            }else if ([UserDefaultSize integerForKey:YZZX_guanzhu_pindao])
            {
                int Tagnsinter  = (int)[UserDefaultSize integerForKey:YZZX_guanzhu_pindao];
                
                NSIndexPath *paths = [NSIndexPath indexPathForRow:Tagnsinter inSection:0];
                [TableviewMassge selectRowAtIndexPath:paths animated:YES scrollPosition:UITableViewScrollPositionNone];

            }
            
   
   
        }

        
    }
        
}




- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{





}




#pragma arguments - mark  UITableViewDelegate
// 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return ArryDataCategory.count;
}


//设置每行高度（每行高度可以不一样）
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 50;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"WebslitID";
    WebslitCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"WebslitCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
        
        
    }
    
    if(ArryDataCategory.count<1)
    {
        return cell;
    }
    CategoryDataModel *cellmodel = [ArryDataCategory objectAtIndex:indexPath.row];
    
    [cell.MyImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.hellomommy.cn%@",cellmodel.image_url]] placeholderImage:[UIImage imageNamed:@"bg"]];

    
    [cell.MyLableTitle setText:cellmodel.cate_name];
    
    return cell;
}


//响应点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row<ArryDataCategory.count)
    { 
    [self.Leftdelegate ChangMainViewContorllerDataAndUIModelTag:indexPath.row];
    }
    

}





@end
